package com.ggk911.pharmacy_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ggk911.pharmacy_system.entity.PurchaseInfo;
import com.ggk911.pharmacy_system.mapper.PurchaseInfoMapper;
import com.ggk911.pharmacy_system.mapper.PurchaseMapper;
import com.ggk911.pharmacy_system.service.IPurchaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ggk-911
 * @version 1.0 2023/3/20 10:15
 */
@Service
public class PurchaseInfoServiceImpl extends ServiceImpl<PurchaseInfoMapper, PurchaseInfo> implements IPurchaseInfoService {

    /**
     * 重写的page方法
     *
     * @param page
     * @param queryWrapper
     * @param <E>
     * @return
     */
    @Override
    public <E extends IPage<PurchaseInfo>> E page(E page, Wrapper<PurchaseInfo> queryWrapper) {
        return baseMapper.diyPage(page, queryWrapper);
    }

    /**
     * 重写save方法自定义重复项更新
     *
     * @param entity
     * @return
     */
    @Override
    public boolean save(PurchaseInfo entity) {
        //重复项
        if (baseMapper.selectByPurchaseIdAndSupplierDrugId(entity.getPurchaseId(), entity.getSupplierDrugId()) != null) {
            return super.updateById(entity);
        }
        return super.save(entity);
    }

    /**
     * 重写的list方法，加强了Map
     *
     * @param queryWrapper
     * @return
     */
    @Override
    public List<PurchaseInfo> list(Wrapper<PurchaseInfo> queryWrapper) {
        return baseMapper.selectListAd(queryWrapper);
    }

    /**
     * 删除整个订单的详情
     *
     * @param purchaseId
     * @return
     */
    public boolean removeByPurchaseId(Integer purchaseId) {
        return baseMapper.removeByPurchaseId(purchaseId);
    }

    /**
     * 过期处理
     *
     * @param id
     * @return
     */
    public boolean setDrugInfoProductionDate(Integer id) {
        return baseMapper.setDrugInfoProductionDate(id);
    }

    /**
     * 拿到过期天数
     */
    public int getDueDate() {
        return baseMapper.getDueDate();
    }

    /**
     * 修改过期天数
     */
    public boolean updateDueDate(Integer day) {
        return baseMapper.updateDueDate(day);
    }
}
